﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StructureMap.Graph;
using System.ServiceModel;
using Extensions;
using System.ServiceModel.Activation;
using System.Diagnostics;

namespace WillBe.Servicehost
{
    public class StructureMapServiceHostFactory : ServiceHostFactory
    {
        public StructureMapServiceHostFactory()
        {
            StructureMap.ObjectFactory.Initialize(x =>
            {
                x.Scan(s =>
                {
                    s.TheCallingAssembly();
                    s.AssembliesFromApplicationBaseDirectory(z => z.GetName().EndsWith("Clients"));
                    s.AssembliesFromApplicationBaseDirectory(z => z.GetName().EndsWith("Logic"));
                    s.AddAllTypesOf(typeof(System.ServiceModel.ClientBase<>));
                    s.WithDefaultConventions();

                    s.AddAllTypesOf(typeof(WillBe.Framework.WillBeActionBase<>));
                    s.RegisterConcreteTypesAgainstTheFirstInterface();
                });
            });
            Trace.WriteLine(StructureMap.ObjectFactory.WhatDoIHave());
        }

        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new StructureMapServiceHost(serviceType, baseAddresses);
        }
    }
}
